home *** CD-ROM | disk | FTP | other *** search
/ Kompuutteri K-CD 2002 #1 / K-CD_2002-01.iso / Delphi / INSTALL / program files / Borland / Delphi6 / Source / Vcl / StdActns.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2001-05-22  |  26.6 KB  |  1,058 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {  Copyright (c) 1995-2001 Borland Software Corporation }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit StdActns;
  11.  
  12. {$H+,X+}
  13.  
  14. interface
  15.  
  16. uses SysUtils, Classes, ActnList, StdCtrls, Forms, Dialogs;
  17.  
  18. type
  19.  
  20. { Hint actions }
  21.  
  22.   THintAction = class(TCustomAction)
  23.   public
  24.     constructor Create(AOwner: TComponent); override;
  25.   published
  26.     property Hint;
  27.   end;
  28.  
  29. { Edit actions }
  30.  
  31.   TEditAction = class(TAction)
  32.   private
  33.     FControl: TCustomEdit;
  34.     procedure SetControl(Value: TCustomEdit);
  35.   protected
  36.     function GetControl(Target: TObject): TCustomEdit; virtual;
  37.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  38.   public
  39.     function HandlesTarget(Target: TObject): Boolean; override;
  40.     procedure UpdateTarget(Target: TObject); override;
  41.     property Control: TCustomEdit read FControl write SetControl;
  42.   end;
  43.  
  44.   TEditCut = class(TEditAction)
  45.   public
  46.     procedure ExecuteTarget(Target: TObject); override;
  47.   end;
  48.  
  49.   TEditCopy = class(TEditAction)
  50.   public
  51.     procedure ExecuteTarget(Target: TObject); override;
  52.   end;
  53.  
  54.   TEditPaste = class(TEditAction)
  55.   public
  56.     procedure UpdateTarget(Target: TObject); override;
  57.     procedure ExecuteTarget(Target: TObject); override;
  58.   end;
  59.  
  60.   TEditSelectAll = class(TEditAction)
  61.   public
  62.     procedure ExecuteTarget(Target: TObject); override;
  63.     procedure UpdateTarget(Target: TObject); override;
  64.   end;
  65.  
  66.   TEditUndo = class(TEditAction)
  67.   public
  68.     procedure ExecuteTarget(Target: TObject); override;
  69.     procedure UpdateTarget(Target: TObject); override;
  70.   end;
  71.  
  72.   TEditDelete = class(TEditAction)
  73.   public
  74.     procedure ExecuteTarget(Target: TObject); override;
  75.     { UpdateTarget is required because TEditAction.UpdateTarget specifically
  76.       checks to see if the action is TEditCut or TEditCopy }
  77.     procedure UpdateTarget(Target: TObject); override;
  78.   end;
  79.  
  80. { MDI Window actions }
  81.  
  82.   TWindowAction = class(TAction)
  83.   private
  84.     FForm: TForm;
  85.     procedure SetForm(Value: TForm);
  86.   protected
  87.     function GetForm(Target: TObject): TForm; virtual;
  88.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  89.   public
  90.     constructor Create(AOwner: TComponent); override;
  91.     function HandlesTarget(Target: TObject): Boolean; override;
  92.     procedure UpdateTarget(Target: TObject); override;
  93.     property Form: TForm read FForm write SetForm;
  94.   end;
  95.  
  96.   TWindowClose = class(TWindowAction)
  97.   public
  98.     procedure ExecuteTarget(Target: TObject); override;
  99.     procedure UpdateTarget(Target: TObject); override;
  100.   end;
  101.  
  102.   TWindowCascade = class(TWindowAction)
  103.   public
  104.     procedure ExecuteTarget(Target: TObject); override;
  105.   end;
  106.  
  107.   TWindowTileHorizontal = class(TWindowAction)
  108.   public
  109.     procedure ExecuteTarget(Target: TObject); override;
  110.   end;
  111.  
  112.   TWindowTileVertical = class(TWindowAction)
  113.   public
  114.     procedure ExecuteTarget(Target: TObject); override;
  115.   end;
  116.  
  117.   TWindowMinimizeAll = class(TWindowAction)
  118.   public
  119.     procedure ExecuteTarget(Target: TObject); override;
  120.   end;
  121.  
  122.   TWindowArrange = class(TWindowAction)
  123.   public
  124.     procedure ExecuteTarget(Target: TObject); override;
  125.   end;
  126.  
  127. { Help actions }
  128.  
  129.   THelpAction = class(TAction)
  130.   public
  131.     constructor Create(AOwner: TComponent); override;
  132.     function HandlesTarget(Target: TObject): Boolean; override;
  133.     procedure UpdateTarget(Target: TObject); override;
  134.   end;
  135.  
  136.   THelpContents = class(THelpAction)
  137.   public
  138.     procedure ExecuteTarget(Target: TObject); override;
  139.   end;
  140.  
  141.   THelpTopicSearch = class(THelpAction)
  142.   public
  143.     procedure ExecuteTarget(Target: TObject); override;
  144.   end;
  145.  
  146.   THelpOnHelp = class(THelpAction)
  147.   public
  148.     procedure ExecuteTarget(Target: TObject); override;
  149.   end;
  150.  
  151.   THelpContextAction = class(THelpAction)
  152.   public
  153.     procedure ExecuteTarget(Target: TObject); override;
  154.     procedure UpdateTarget(Target: TObject); override;
  155.   end;
  156.  
  157. { TCommonDialogAction }
  158.  
  159.   TCommonDialogClass = class of TCommonDialog;
  160.  
  161.   TCommonDialogAction = class(TCustomAction)
  162.   private
  163.     FExecuteResult: Boolean;
  164.     FOnAccept: TNotifyEvent;
  165.     FOnCancel: TNotifyEvent;
  166.     FBeforeExecute: TNotifyEvent;
  167.   protected
  168.     FDialog: TCommonDialog;
  169.     function GetDialogClass: TCommonDialogClass; virtual;
  170.   public
  171.     constructor Create(AOwner: TComponent); override;
  172.     function Handlestarget(Target: TObject): Boolean; override;
  173.     procedure ExecuteTarget(Target: TObject); override;
  174.     property ExecuteResult: Boolean read FExecuteResult;
  175.     property BeforeExecute: TNotifyEvent read FBeforeExecute write FBeforeExecute;
  176.     property OnAccept: TNotifyEvent read FOnAccept write FOnAccept;
  177.     property OnCancel: TNotifyEvent read FOnCancel write FOnCancel;
  178.   end;
  179.  
  180. { File Actions }
  181.  
  182.   TFileAction = class(TCommonDialogAction)
  183.   private
  184.     function GetFileName: TFileName;
  185.     procedure SetFileName(const Value: TFileName);
  186.   protected
  187.     function GetDialog: TOpenDialog;
  188.     property FileName: TFileName read GetFileName write SetFileName;
  189.   end;
  190.  
  191.   TFileOpen = class(TFileAction)
  192.   protected
  193.     function GetDialogClass: TCommonDialogClass; override;
  194.   public
  195.     constructor Create(AOwner: TComponent); override;
  196.   published
  197.     property Caption;
  198.     property Dialog: TOpenDialog read GetDialog;
  199.     property Enabled;
  200.     property HelpContext;
  201.     property Hint;
  202.     property ImageIndex;
  203.     property ShortCut;
  204.     property SecondaryShortCuts;
  205.     property Visible;
  206.     property BeforeExecute;
  207.     property OnAccept;
  208.     property OnCancel;
  209.     property OnHint;
  210.   end;
  211.  
  212.   TFileOpenWith = class(TFileOpen)
  213.   private
  214.     FFileName: TFileName;
  215.   public
  216.     procedure ExecuteTarget(Target: TObject); override;
  217.   published
  218.     property FileName: TFileName read FFileName write FFileName;
  219.     property BeforeExecute;
  220.   end;
  221.  
  222.   TFileSaveAs = class(TFileAction)
  223.   private
  224.     function GetSaveDialog: TSaveDialog;
  225.   protected
  226.     function GetDialogClass: TCommonDialogClass; override;
  227.   public
  228.     constructor Create(AOwner: TComponent); override;
  229.   published
  230.     property Caption;
  231.     property Dialog: TSaveDialog read GetSaveDialog;
  232.     property Enabled;
  233.     property HelpContext;
  234.     property Hint;
  235.     property ImageIndex;
  236.     property ShortCut;
  237.     property SecondaryShortCuts;
  238.     property Visible;
  239.     property BeforeExecute;
  240.     property OnAccept;
  241.     property OnCancel;
  242.     property OnHint;
  243.   end;
  244.  
  245.   TFilePrintSetup = class(TCommonDialogAction)
  246.   private
  247.     function GetDialog: TPrinterSetupDialog;
  248.   protected
  249.     function GetDialogClass: TCommonDialogClass; override;
  250.   published
  251.     property Caption;
  252.     property Dialog: TPrinterSetupDialog read GetDialog;
  253.     property Enabled;
  254.     property HelpContext;
  255.     property Hint;
  256.     property ImageIndex;
  257.     property ShortCut;
  258.     property SecondaryShortCuts;
  259.     property Visible;
  260.     property BeforeExecute;
  261.     property OnAccept;
  262.     property OnCancel;
  263.     property OnHint;
  264.   end;
  265.  
  266.   TFileExit = class(TCustomAction)
  267.   public
  268.     function HandlesTarget(Target: TObject): Boolean; override;
  269.     procedure ExecuteTarget(Target: TObject); override;
  270.   published
  271.     property Caption;
  272.     property Enabled;
  273.     property HelpContext;
  274.     property Hint;
  275.     property ImageIndex;
  276.     property ShortCut;
  277.     property SecondaryShortCuts;
  278.     property Visible;
  279.     property OnHint;
  280.   end;
  281.  
  282. { Search Actions }
  283.  
  284.   TSearchAction = class(TCommonDialogAction)
  285.   protected
  286.     FControl: TCustomEdit;
  287.     FFindFirst: Boolean;
  288.   public
  289.     constructor Create(AOwner: TComponent); override;
  290.     function HandlesTarget(Target: TObject): Boolean; override;
  291.     procedure Search(Sender: TObject); virtual;
  292.     procedure UpdateTarget(Target: TObject); override;
  293.     procedure ExecuteTarget(Target: TObject); override;
  294.   end;
  295.  
  296.   TSearchFind = class(TSearchAction)
  297.   private
  298.     function GetFindDialog: TFindDialog;
  299.   protected
  300.     function GetDialogClass: TCommonDialogClass; override;
  301.   published
  302.     property Caption;
  303.     property Dialog: TFindDialog read GetFindDialog;
  304.     property Enabled;
  305.     property HelpContext;
  306.     property Hint;
  307.     property ImageIndex;
  308.     property ShortCut;
  309.     property SecondaryShortCuts;
  310.     property Visible;
  311.     property BeforeExecute;
  312.     property OnAccept;
  313.     property OnCancel;
  314.     property OnHint;
  315.   end;
  316.  
  317.   TSearchReplace = class(TSearchAction)
  318.   private
  319.     procedure Replace(Sender: TObject);
  320.     function GetReplaceDialog: TReplaceDialog;
  321.   protected
  322.     function GetDialogClass: TCommonDialogClass; override;
  323.   public
  324.     procedure ExecuteTarget(Target: TObject); override;
  325.   published
  326.     property Caption;
  327.     property Dialog: TReplaceDialog read GetReplaceDialog;
  328.     property Enabled;
  329.     property HelpContext;
  330.     property Hint;
  331.     property ImageIndex;
  332.     property ShortCut;
  333.     property SecondaryShortCuts;
  334.     property Visible;
  335.     property BeforeExecute;
  336.     property OnAccept;
  337.     property OnCancel;
  338.     property OnHint;
  339.   end;
  340.  
  341.   TSearchFindFirst = class(TSearchFind)
  342.   public
  343.     constructor Create(AOwner: TComponent); override;
  344.   end;
  345.  
  346.   TSearchFindNext = class(TCustomAction)
  347.   private
  348.     FSearchFind: TSearchFind;
  349.   public
  350.     constructor Create(AOwner: TComponent); override;
  351.     function HandlesTarget(Target: TObject): Boolean; override;
  352.     procedure UpdateTarget(Target: TObject); override;
  353.     procedure ExecuteTarget(Target: TObject); override;
  354.   published
  355.     property Caption;
  356.     property Enabled;
  357.     property HelpContext;
  358.     property Hint;
  359.     property ImageIndex;
  360.     property SearchFind: TSearchFind read FSearchFind write FSearchFind;
  361.     property ShortCut;
  362.     property SecondaryShortCuts;
  363.     property Visible;
  364.     property OnHint;
  365.   end;
  366.  
  367. { TFontEdit }
  368.  
  369.   TFontEdit = class(TCommonDialogAction)
  370.   private
  371.     function GetDialog: TFontDialog;
  372.   protected
  373.     function GetDialogClass: TCommonDialogClass; override;
  374.   published
  375.     property Caption;
  376.     property Dialog: TFontDialog read GetDialog;
  377.     property Enabled;
  378.     property HelpContext;
  379.     property Hint;
  380.     property ImageIndex;
  381.     property ShortCut;
  382.     property SecondaryShortCuts;
  383.     property Visible;
  384.     property BeforeExecute;
  385.     property OnAccept;
  386.     property OnCancel;
  387.     property OnHint;
  388.   end;
  389.  
  390. { TColorSelect }
  391.  
  392.   TColorSelect = class(TCommonDialogAction)
  393.   private
  394.     function GetDialog: TColorDialog;
  395.   protected
  396.     function GetDialogClass: TCommonDialogClass; override;
  397.   published
  398.     property Caption;
  399.     property Dialog: TColorDialog read GetDialog;
  400.     property Enabled;
  401.     property HelpContext;
  402.     property Hint;
  403.     property ImageIndex;
  404.     property ShortCut;
  405.     property SecondaryShortCuts;
  406.     property Visible;
  407.     property BeforeExecute;
  408.     property OnAccept;
  409.     property OnCancel;
  410.     property OnHint;
  411.   end;
  412.  
  413. { TPrintDlg }
  414.  
  415.   TPrintDlg = class(TCommonDialogAction)
  416.   private
  417.     function GetDialog: TPrintDialog;
  418.   protected
  419.     function Getdialogclass: TCommonDialogClass; override;
  420.   published
  421.     property Caption;
  422.     property Dialog: TPrintDialog read GetDialog;
  423.     property Enabled;
  424.     property HelpContext;
  425.     property Hint;
  426.     property ImageIndex;
  427.     property ShortCut;
  428.     property SecondaryShortCuts;
  429.     property Visible;
  430.     property BeforeExecute;
  431.     property OnAccept;
  432.     property OnCancel;
  433.     property OnHint;
  434.   end;
  435.  
  436. implementation
  437.  
  438. uses Windows, Messages, Consts, Clipbrd, StrUtils, ShellAPI;
  439.  
  440. { THintAction }
  441.  
  442. constructor THintAction.Create(AOwner: TComponent);
  443. begin
  444.   inherited Create(AOwner);
  445.   DisableIfNoHandler := False;
  446. end;
  447.  
  448. { TEditAction }
  449.  
  450. { TEditAction }
  451.  
  452. function TEditAction.GetControl(Target: TObject): TCustomEdit;
  453. begin
  454.   { We could hard cast Target as a TCustomEdit since HandlesTarget "should" be
  455.     called before ExecuteTarget and UpdateTarget, however, we're being safe. }
  456.   Result := Target as TCustomEdit;
  457. end;
  458.  
  459. function TEditAction.HandlesTarget(Target: TObject): Boolean;
  460. begin
  461.   Result := ((Control <> nil) and (Target = Control) or
  462.     (Control = nil) and (Target is TCustomEdit)) and TCustomEdit(Target).Focused;
  463. end;
  464.  
  465. procedure TEditAction.Notification(AComponent: TComponent;
  466.   Operation: TOperation);
  467. begin
  468.   inherited Notification(AComponent, Operation);
  469.   if (Operation = opRemove) and (AComponent = Control) then Control := nil;
  470. end;
  471.  
  472. procedure TEditAction.UpdateTarget(Target: TObject);
  473. begin
  474.   if (Self is TEditCut) or (Self is TEditCopy) then
  475.     Enabled := GetControl(Target).SelLength > 0;
  476. end;
  477.  
  478. procedure TEditAction.SetControl(Value: TCustomEdit);
  479. begin
  480.   if Value <> FControl then
  481.   begin
  482.     FControl := Value;
  483.     if Value <> nil then Value.FreeNotification(Self);
  484.   end;
  485. end;
  486.  
  487. { TEditCopy }
  488.  
  489. procedure TEditCopy.ExecuteTarget(Target: TObject);
  490. begin
  491.   GetControl(Target).CopyToClipboard;
  492. end;
  493.  
  494. { TEditCut }
  495.  
  496. procedure TEditCut.ExecuteTarget(Target: TObject);
  497. begin
  498.   GetControl(Target).CutToClipboard;
  499. end;
  500.  
  501. { TEditPaste }
  502.  
  503. procedure TEditPaste.ExecuteTarget(Target: TObject);
  504. begin
  505.   GetControl(Target).PasteFromClipboard;
  506. end;
  507.  
  508. procedure TEditPaste.UpdateTarget(Target: TObject);
  509. begin
  510.   Enabled := Clipboard.HasFormat(CF_TEXT);
  511. end;
  512.  
  513. { TEditSelectAll }
  514.  
  515. procedure TEditSelectAll.ExecuteTarget(Target: TObject);
  516. begin
  517.   GetControl(Target).SelectAll;
  518. end;
  519.  
  520. procedure TEditSelectAll.UpdateTarget(Target: TObject);
  521. begin
  522.   Enabled := Length(GetControl(Target).Text) > 0;
  523. end;
  524.  
  525. { TEditUndo }
  526.  
  527. procedure TEditUndo.ExecuteTarget(Target: TObject);
  528. begin
  529.   GetControl(Target).Undo;
  530. end;
  531.  
  532. procedure TEditUndo.UpdateTarget(Target: TObject);
  533. begin
  534.   Enabled := GetControl(Target).CanUndo;
  535. end;
  536.  
  537. { TEditDelete }
  538.  
  539. procedure TEditDelete.ExecuteTarget(Target: TObject);
  540. begin
  541.   GetControl(Target).ClearSelection;
  542. end;
  543.  
  544. procedure TEditDelete.UpdateTarget(Target: TObject);
  545. begin
  546.   Enabled := GetControl(Target).SelLength > 0;
  547. end;
  548.  
  549. { TWindowAction }
  550.  
  551. function TWindowAction.GetForm(Target: TObject): TForm;
  552. begin
  553.   { We could hard cast Target as a TForm since HandlesTarget "should" be called
  554.     before ExecuteTarget and UpdateTarget, however, we're being safe. }
  555.   Result := (Target as TForm);
  556. end;
  557.  
  558. function TWindowAction.HandlesTarget(Target: TObject): Boolean;
  559. begin
  560.   Result := ((Form <> nil) and (Target = Form) or
  561.     (Form = nil) and (Target is TForm)) and
  562.     (TForm(Target).FormStyle = fsMDIForm);
  563. end;
  564.  
  565. procedure TWindowAction.Notification(AComponent: TComponent;
  566.   Operation: TOperation);
  567. begin
  568.   inherited Notification(AComponent, Operation);
  569.   if (Operation = opRemove) and (AComponent = Form) then Form := nil;
  570. end;
  571.  
  572. procedure TWindowAction.UpdateTarget(Target: TObject);
  573. begin
  574.   Enabled := GetForm(Target).MDIChildCount > 0;
  575. end;
  576.  
  577. procedure TWindowAction.SetForm(Value: TForm);
  578. begin
  579.   if Value <> FForm then
  580.   begin
  581.     FForm := Value;
  582.     if Value <> nil then Value.FreeNotification(Self);
  583.   end;
  584. end;
  585.  
  586. constructor TWindowAction.Create(AOwner: TComponent);
  587. begin
  588.   inherited Create(AOwner);
  589.   DisableIfNoHandler := False;
  590.   Enabled := csDesigning in ComponentState;
  591. end;
  592.  
  593. { TWindowClose }
  594.  
  595. procedure TWindowClose.ExecuteTarget(Target: TObject);
  596. begin
  597.   with GetForm(Target) do
  598.     if ActiveMDIChild <> nil then ActiveMDIChild.Close;
  599. end;
  600.  
  601. procedure TWindowClose.UpdateTarget(Target: TObject);
  602. begin
  603.   Enabled := GetForm(Target).ActiveMDIChild <> nil;
  604. end;
  605.  
  606. { TWindowCascade }
  607.  
  608. procedure TWindowCascade.ExecuteTarget(Target: TObject);
  609. begin
  610.   GetForm(Target).Cascade;
  611. end;
  612.  
  613. { TWindowTileHorizontal }
  614.  
  615. procedure DoTile(Form: TForm; TileMode: TTileMode);
  616. const
  617.   TileParams: array[TTileMode] of Word = (MDITILE_HORIZONTAL, MDITILE_VERTICAL);
  618. begin
  619.   if (Form.FormStyle = fsMDIForm) and (Form.ClientHandle <> 0) then
  620.     SendMessage(Form.ClientHandle, WM_MDITILE, TileParams[TileMode], 0);
  621. end;
  622.  
  623. procedure TWindowTileHorizontal.ExecuteTarget(Target: TObject);
  624. begin
  625.   DoTile(GetForm(Target), tbHorizontal);
  626. end;
  627.  
  628. { TWindowTileVertical }
  629.  
  630. procedure TWindowTileVertical.ExecuteTarget(Target: TObject);
  631. begin
  632.   DoTile(GetForm(Target), tbVertical);
  633. end;
  634.  
  635. { TWindowMinimizeAll }
  636.  
  637. procedure TWindowMinimizeAll.ExecuteTarget(Target: TObject);
  638. var
  639.   I: Integer;
  640. begin
  641.   { Must be done backwards through the MDIChildren array }
  642.   with GetForm(Target) do
  643.     for I := MDIChildCount - 1 downto 0 do
  644.       MDIChildren[I].WindowState := wsMinimized;
  645. end;
  646.  
  647. { TWindowArrange }
  648.  
  649. procedure TWindowArrange.ExecuteTarget(Target: TObject);
  650. begin
  651.   GetForm(Target).ArrangeIcons;
  652. end;
  653.  
  654. { THelpAction }
  655.  
  656. constructor THelpAction.Create(AOwner: TComponent);
  657. begin
  658.   inherited Create(AOwner);
  659.   DisableIfNoHandler := False;
  660.   Enabled := csDesigning in ComponentState;
  661. end;
  662.  
  663. function THelpAction.HandlesTarget(Target: TObject): Boolean;
  664. begin
  665.   Result := True;
  666. end;
  667.  
  668. procedure THelpAction.UpdateTarget(Target: TObject);
  669. begin
  670.   Enabled := Assigned(Application);
  671. end;
  672.  
  673. { THelpContents }
  674.  
  675. procedure THelpContents.ExecuteTarget(Target: TObject);
  676. begin
  677.   Application.HelpCommand(HELP_FINDER, 0);
  678. end;
  679.  
  680. { THelpTopicSearch }
  681.  
  682. procedure THelpTopicSearch.ExecuteTarget(Target: TObject);
  683. begin
  684.   Application.HelpCommand(HELP_FINDER, Integer(PChar('')));
  685. end;
  686.  
  687. { THelpOnHelp }
  688.  
  689. procedure THelpOnHelp.ExecuteTarget(Target: TObject);
  690. begin
  691.   Application.HelpCommand(HELP_HELPONHELP, Integer(PChar('')));
  692. end;
  693.  
  694. { THelpContextAction }
  695.  
  696. procedure THelpContextAction.ExecuteTarget(Target: TObject);
  697. begin
  698.   Application.HelpCommand(HELP_CONTEXT, Screen.ActiveControl.HelpContext);
  699. end;
  700.  
  701. procedure THelpContextAction.UpdateTarget(Target: TObject);
  702. begin
  703.   Enabled := Assigned(Screen) and Assigned(Screen.ActiveControl) and
  704.     (Screen.ActiveControl.HelpContext <> 0);
  705. end;
  706.  
  707. { TCommonDialogAction }
  708.  
  709. constructor TCommonDialogAction.Create(AOwner: TComponent);
  710. var
  711.   DialogClass: TCommonDialogClass;
  712. begin
  713.   inherited Create(AOwner);
  714.   DialogClass := GetDialogClass;
  715.   if Assigned(DialogClass) then
  716.   begin
  717.     FDialog := DialogClass.Create(Self);
  718.     FDialog.SetSubComponent(True);
  719.   end;
  720.   DisableIfNoHandler := False;
  721.   Enabled := True;
  722. end;
  723.  
  724. procedure TCommonDialogAction.ExecuteTarget(Target: TObject);
  725. begin
  726.   FExecuteResult := False;
  727.   if Assigned(FDialog) then
  728.   begin
  729.     if Assigned(FBeforeExecute) then
  730.       FBeforeExecute(Self);  
  731.     FExecuteResult := FDialog.Execute;
  732.     if FExecuteResult then
  733.     begin
  734.       if Assigned(FOnAccept) then
  735.         FOnAccept(Self)
  736.     end
  737.     else
  738.       if Assigned(FOnCancel) then
  739.         FOnCancel(Self);
  740.   end;
  741. end;
  742.  
  743. function TCommonDialogAction.GetDialogClass: TCommonDialogClass;
  744. begin
  745.   Result := nil;
  746. end;
  747.  
  748. function TCommonDialogAction.Handlestarget(Target: TObject): Boolean;
  749. begin
  750.   Result := True;
  751. end;
  752.  
  753. { TFileAction }
  754.  
  755. function TFileAction.GetDialog: TOpenDialog;
  756. begin
  757.   Result := TOpenDialog(FDialog);
  758. end;
  759.  
  760. function TFileAction.GetFileName: TFileName;
  761. begin
  762.   Result := GetDialog.FileName;
  763. end;
  764.  
  765. procedure TFileAction.SetFileName(const Value: TFileName);
  766. begin
  767.   GetDialog.FileName := Value;
  768. end;
  769.  
  770. { TFileOpen }
  771.  
  772. constructor TFileOpen.Create(AOwner: TComponent);
  773. begin
  774.   inherited Create(AOwner);
  775.   FDialog.Name := 'OpenDialog';
  776. end;
  777.  
  778. function TFileOpen.GetDialogClass: TCommonDialogClass;
  779. begin
  780.   Result := TOpenDialog;
  781. end;
  782.  
  783. { TFileOpenWith }
  784.  
  785. procedure TFileOpenWith.ExecuteTarget(Target: TObject);
  786. begin
  787.   if (Length(FFileName) = 0) or not FileExists(FFileName) then
  788.     inherited;
  789.   FFileName := Dialog.FileName;
  790.   if FExecuteResult then
  791.     ShellExecute(0, 'open', 'rundll32.exe',                            { do not localize}
  792.       PChar(Format('shell32.dll,OpenAs_RunDLL %s', [FFileName])), nil, { do not localize}
  793.       SW_SHOW);
  794. end;
  795.  
  796. { TFileSaveAs }
  797.  
  798. constructor TFileSaveAs.Create(AOwner: TComponent);
  799. begin
  800.   inherited Create(AOwner);
  801.   FDialog.Name := 'SaveDialog';
  802. end;
  803.  
  804. function TFileSaveAs.GetDialogClass: TCommonDialogClass;
  805. begin
  806.   Result := TSaveDialog;
  807. end;
  808.  
  809. function TFileSaveAs.GetSaveDialog: TSaveDialog;
  810. begin
  811.   Result := TSaveDialog(FDialog);
  812. end;
  813.  
  814. { TFilePrintSetup }
  815.  
  816. function TFilePrintSetup.GetDialog: TPrinterSetupDialog;
  817. begin
  818.   Result := TPrinterSetupDialog(FDialog);
  819. end;
  820.  
  821. function TFilePrintSetup.GetDialogClass: TCommonDialogClass;
  822. begin
  823.   Result := TPrinterSetupDialog;
  824. end;
  825.  
  826. { TFileExit }
  827.  
  828. procedure TFileExit.ExecuteTarget(Target: TObject);
  829. begin
  830.   if Assigned(Application.MainForm) then
  831.   begin
  832.     Application.HelpCommand(HELP_QUIT, 0);
  833.     Application.MainForm.Close;
  834.   end;
  835. end;
  836.  
  837. function TFileExit.HandlesTarget(Target: TObject): Boolean;
  838. begin
  839.   Result := True;
  840. end;
  841.  
  842. { SearchEdit scans the text of a TCustomEdit-derived component for a given
  843.   search string.  The search starts at the current caret position in the
  844.   control unless FindFirst is true then the search starts at the beginning.
  845.   The Options parameter determines whether the search runs forward
  846.   (frDown) or backward from the caret position, whether or not the text
  847.   comparison is case sensitive, and whether the matching string must be a
  848.   whole word.  If text is already selected in the control, the search starts
  849.   at the 'far end' of the selection (SelStart if searching backwards, SelEnd
  850.   if searching forwards).  If a match is found, the control's text selection
  851.   is changed to select the found text and the function returns True.  If no
  852.   match is found, the function returns False. }
  853.  
  854. function SearchEdit(EditControl: TCustomEdit; const SearchString: String;
  855.   Options: TFindOptions; FindFirst: Boolean = False): Boolean;
  856. var
  857.   Buffer, P: PChar;
  858.   Size: Word;
  859.   SearchOptions: TStringSearchOptions;
  860. begin
  861.   Result := False;
  862.   if (Length(SearchString) = 0) then Exit;
  863.   Size := EditControl.GetTextLen;
  864.   if (Size = 0) then Exit;
  865.   Buffer := StrAlloc(Size + 1);
  866.   try
  867.     SearchOptions := [];
  868.     if frDown in Options then
  869.       Include(SearchOptions, soDown);
  870.     if frMatchCase in Options then
  871.       Include(SearchOptions, soMatchCase);
  872.     if frWholeWord in Options then
  873.       Include(SearchOptions, soWholeWord);
  874.     EditControl.GetTextBuf(Buffer, Size + 1);
  875.     if FindFirst then
  876.       P := SearchBuf(Buffer, Size, 0, EditControl.SelLength,
  877.              SearchString, SearchOptions)
  878.     else
  879.       P := SearchBuf(Buffer, Size, EditControl.SelStart, EditControl.SelLength,
  880.              SearchString, SearchOptions);
  881.     if P <> nil then
  882.     begin
  883.       EditControl.SelStart := P - Buffer;
  884.       EditControl.SelLength := Length(SearchString);
  885.       Result := True;
  886.     end;
  887.   finally
  888.     StrDispose(Buffer);
  889.   end;
  890. end;
  891.  
  892. { TSearchAction }
  893.  
  894. constructor TSearchAction.Create(AOwner: TComponent);
  895. begin
  896.   inherited Create(AOwner);
  897.   TFindDialog(FDialog).OnFind := Search;
  898.   FFindFirst := False;
  899. end;
  900.  
  901. procedure TSearchAction.ExecuteTarget(Target: TObject);
  902. begin
  903.   FControl := TCustomEdit(Target);
  904.   inherited ExecuteTarget(Target);
  905. end;
  906.  
  907. function TSearchAction.HandlesTarget(Target: TObject): Boolean;
  908. begin
  909.   Result := Screen.ActiveControl is TCustomEdit;
  910.   if not Result then
  911.     Enabled := False;
  912. end;
  913.  
  914. procedure TSearchAction.Search(Sender: TObject);
  915. begin
  916.   // FControl gets set in ExecuteTarget
  917.   if Assigned(FControl) then
  918.     if not SearchEdit(FControl, TFindDialog(FDialog).FindText,
  919.        TFindDialog(FDialog).Options, FFindFirst) then
  920.       ShowMessage(Format(STextNotFound, [TFindDialog(FDialog).FindText]));
  921.   FControl := nil;
  922. end;
  923.  
  924. procedure TSearchAction.UpdateTarget(Target: TObject);
  925. begin
  926.   Enabled := Target is TCustomEdit and
  927.     (TCustomEdit(Screen.ActiveControl).GetTextLen > 0);
  928. end;
  929.  
  930. { TSearchFind }
  931.  
  932. function TSearchFind.GetDialogClass: TCommonDialogClass;
  933. begin
  934.   Result := TFindDialog;
  935. end;
  936.  
  937. function TSearchFind.GetFindDialog: TFindDialog;
  938. begin
  939.   Result := TFindDialog(FDialog);
  940. end;
  941.  
  942. { TSearchReplace }
  943.  
  944. procedure TSearchReplace.ExecuteTarget(Target: TObject);
  945. begin
  946.   inherited ExecuteTarget(Target);
  947.   TReplaceDialog(FDialog).OnReplace := Replace;
  948. end;
  949.  
  950. function TSearchReplace.GetDialogClass: TCommonDialogClass;
  951. begin
  952.   Result := TReplaceDialog;
  953. end;
  954.  
  955. function TSearchReplace.GetReplaceDialog: TReplaceDialog;
  956. begin
  957.   Result := TReplaceDialog(FDialog);
  958. end;
  959.  
  960. procedure TSearchReplace.Replace(Sender: TObject);
  961. var
  962.   Found: Boolean;
  963. begin
  964.   if Assigned(FControl) then
  965.     with Sender as TReplaceDialog do
  966.     begin
  967.       if AnsiCompareText(FControl.SelText, FindText) = 0 then
  968.         FControl.SelText := ReplaceText;
  969.       Found := SearchEdit(FControl, Dialog.FindText, Dialog.Options, FFindFirst);
  970.       while Found and (frReplaceAll in Dialog.Options) do
  971.       begin
  972.         FControl.SelText := ReplaceText;
  973.         Found := SearchEdit(FControl, Dialog.FindText, Dialog.Options, FFindFirst);
  974.       end;
  975.       if (not Found) and (frReplace in Dialog.Options) then
  976.         ShowMessage(Format(STextNotFound, [Dialog.FindText]));
  977.     end;
  978.   FControl := nil;
  979. end;
  980.  
  981. { TSearchFindFirst }
  982.  
  983. constructor TSearchFindFirst.Create(AOwner: TComponent);
  984. begin
  985.   inherited Create(AOwner);
  986.   FFindFirst := True;
  987. end;
  988.  
  989. { TSearchFindNext }
  990.  
  991. constructor TSearchFindNext.Create(AOwner: TComponent);
  992. begin
  993.   inherited Create(AOwner);
  994.   DisableIfNoHandler := False;
  995.   Enabled := csDesigning in ComponentState;
  996. end;
  997.  
  998. procedure TSearchFindNext.ExecuteTarget(Target: TObject);
  999. begin
  1000.   if not Assigned(FSearchFind) then exit;
  1001.   FSearchFind.Search(Target);
  1002. end;
  1003.  
  1004. function TSearchFindNext.HandlesTarget(Target: TObject): Boolean;
  1005. begin
  1006.   if Assigned(FSearchFind) then
  1007.     Result := (FSearchFind.Enabled) and
  1008.       (Length(TFindDialog(FSearchFind.Dialog).FindText) > 0)
  1009.   else
  1010.     Result := False;
  1011. end;
  1012.  
  1013. procedure TSearchFindNext.UpdateTarget(Target: TObject);
  1014. begin
  1015.   if Assigned(FSearchFind) then
  1016.     Enabled := Length(TFindDialog(FSearchFind.Dialog).FindText) > 0
  1017.   else
  1018.     Enabled := False;
  1019. end;
  1020.  
  1021. { TFontEdit }
  1022.  
  1023. function TFontEdit.GetDialog: TFontDialog;
  1024. begin
  1025.   Result := TFontDialog(FDialog);
  1026. end;
  1027.  
  1028. function TFontEdit.GetDialogClass: TCommonDialogClass;
  1029. begin
  1030.   Result := TFontDialog;
  1031. end;
  1032.  
  1033. { TColorSelect }
  1034.  
  1035. function TColorSelect.GetDialog: TColorDialog;
  1036. begin
  1037.   Result := TColorDialog(FDialog);
  1038. end;
  1039.  
  1040. function TColorSelect.GetDialogClass: TCommonDialogClass;
  1041. begin
  1042.   Result := TColorDialog;
  1043. end;
  1044.  
  1045. { TPrintDlg }
  1046.  
  1047. function TPrintDlg.GetDialog: TPrintDialog;
  1048. begin
  1049.   Result := TPrintDialog(FDialog);
  1050. end;
  1051.  
  1052. function TPrintDlg.Getdialogclass: TCommonDialogClass;
  1053. begin
  1054.   Result := TPrintDialog;
  1055. end;
  1056.  
  1057. end.
  1058.